<template>
  <a-modal
    title="新建返回体"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-row :gutter="24">
          <a-col
            v-for="item in formConfig"
            :key="item.key"
            :span="12">
            <a-form-item :label="item.label">
              <a-input v-decorator="[item.key, {rules: []}]"/>
            </a-form-item>
          </a-col>
        </a-row>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  // import pick from 'lodash.pick'

  // 表单字段
  import { addMockInfo } from '../../../api/mock-info'

  export default {
    props: {
      visible: {
        type: Boolean,
        required: true
      },
      loading: {
        type: Boolean,
        default: () => false
      },
      model: {
        type: Object,
        default: () => null
      }
    },
    data () {
      this.formLayout = {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 }
        }
      }
      return {
        formConfig: [
          { key: 'project', label: '返回体名称' },
          { key: 'retData', label: '返回体' },
          { key: 'status', label: '状态' },
          { key: 'description', label: '描述' }
          ],
        form: this.$form.createForm(this)
      }
    },
    created () {
      console.log('custom modal created')

      // 防止表单未注册
      this.formConfig.forEach(v => this.form.getFieldDecorator(v.key))

      // // 当 model 发生改变时，为表单设置值
      // this.$watch('model', () => {
      //   this.model && this.form.setFieldsValue(pick(this.model, fields))
      // })
    },
    methods: {
      add () {
        this.form.validateFields((err, values) => {
          if (!err) {
            console.log('Received values of form: ', values)
            addMockInfo(values).then((res) => {
              console.log(res)
            })
          }
        })
      }
    }
  }
</script>
